﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using SIMS.Shared.DTO;
using System;
using System.Threading.Tasks;
using SIMS.Common.Models;
using SIMS.Common.RestClient;
using SIMS.Shared.Models;

namespace SIMS.CourseModule.ViewModels
{
    public class AddEditCourseViewModel: BindableBase, IDialogAware
    {
        private readonly HttpRestClient _httpClient;

        public AddEditCourseViewModel(HttpRestClient httpClient)
        {
            _httpClient = httpClient;
        }

        private CourseDTO course;

        public CourseDTO Course
        {
            get { return course; }
            set { SetProperty(ref course, value); }
        }

        private DelegateCommand loadedCommand;

        public DelegateCommand LoadedCommand
        {
            get
            {
                if (loadedCommand == null)
                {
                    loadedCommand = new DelegateCommand(Loaded);
                }
                return loadedCommand;
            }
        }

        private void Loaded()
        {
        }

        private DelegateCommand cancelCommand;

        public DelegateCommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new DelegateCommand(Cancel);
                }
                return cancelCommand;
            }
        }

        private void Cancel()
        {
            RequestClose?.Invoke((new DialogResult(ButtonResult.Cancel)));
        }

        private DelegateCommand saveCommand;

        public DelegateCommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new DelegateCommand(Save);
                }
                return saveCommand;
            }
        }

        private async void Save()
        {
            if (Course != null)
            {
                Course.CreateTime = DateTime.Now;
                Course.LastEditTime = DateTime.Now;
                
                bool flag = false;
                if (Course.Id > 0)
                {
                    flag = await UpdateCourse(Course);
                }
                else
                {
                    flag = await AddCourse(Course);
                }
                if (flag)
                {
                    RequestClose?.Invoke((new DialogResult(ButtonResult.OK)));
                }
            }
        }

        private async Task<bool> AddCourse(CourseDTO course)
        {
            ApiRequest request = new ApiRequest();
            request.Method = RestSharp.Method.Post;
            request.Parameters = course;
            request.Route = Api.COURSE_ADDCOURSE;
            ResponseResultModel response = await _httpClient.Execute(request);
            if (response.code == 1)
            {
                long r = (long)response.data;
                return r == 1;
            }
            return false;
        }

        private async Task<bool> UpdateCourse(CourseDTO course)
        {
            ApiRequest request = new ApiRequest();
            request.Method = RestSharp.Method.Put;
            request.Parameters = course;
            request.Route = Api.COURSE_UPDATECOURSE;
            ResponseResultModel response = await _httpClient.Execute(request);
            if (response.code == 1)
            {
                long r = (long)response.data;
                return r == 1;
            }
            return false;
        }

        public string Title => "新增或编辑课程信息";

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null && parameters.ContainsKey("course"))
            {
                var course = parameters.GetValue<CourseDTO>("course");
                this.Course = (CourseDTO)course.Clone();
            }
            else
            {
                this.Course = new CourseDTO();
            }
        }
    }
}
